package cn.sibu.disalllowtouchintercept;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

/**
 * Created by 刘红亮 on 15-12-23 下午5:23.
 */
public class DrawableDragShadowBuilder extends View.DragShadowBuilder {

    private Drawable mDrawable;

    public DrawableDragShadowBuilder(View view,Drawable drawable){

        super(view);
        //设置Drawable并使用一个绿色的过滤器
        mDrawable = drawable;
        mDrawable.setColorFilter(
                new PorterDuffColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY)
        );
    }

    @Override
    public void onProvideShadowMetrics(Point shadowSize, Point shadowTouchPoint) {
        //填充大小
        shadowSize.x = mDrawable.getIntrinsicWidth();
        shadowSize.y = mDrawable.getIntrinsicHeight();
        //设置阴影相对于触摸位置
        //这里阴影位于手指下方的中心
        shadowTouchPoint.x = mDrawable.getIntrinsicWidth()/2;
        shadowTouchPoint.y = mDrawable.getIntrinsicHeight()/2;

        mDrawable.setBounds(new Rect(0,0,shadowSize.x,shadowSize.y));

    }

    @Override
    public void onDrawShadow(Canvas canvas) {
        //在提供的Canvas上绘制阴影视图
        mDrawable.draw(canvas);
    }
}
